"""
API统一响应封装工具
遵循开发规范，提供标准的API响应格式
"""
from datetime import datetime
from typing import Any, Optional


class ApiResponseHelper:
    """API统一响应助手"""

    @staticmethod
    def success(data: Any = None, msg: str = "操作成功", code: int = 200) -> dict:
        """
        成功响应

        Args:
            data: 响应数据
            msg: 响应消息
            code: 状态码

        Returns:
            标准响应字典
        """
        return {
            "code": code,
            "success": True,
            "msg": msg,
            "data": data,
            "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        }

    @staticmethod
    def error(msg: str, code: int = 500, data: Any = None) -> dict:
        """
        错误响应

        Args:
            msg: 错误消息
            code: 错误码
            data: 附加数据

        Returns:
            标准响应字典
        """
        return {
            "code": code,
            "success": False,
            "msg": msg,
            "data": data,
            "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        }

    @staticmethod
    def not_found(msg: str = "资源不存在") -> dict:
        """404响应"""
        return ApiResponseHelper.error(msg=msg, code=404)

    @staticmethod
    def bad_request(msg: str = "请求参数错误") -> dict:
        """400响应"""
        return ApiResponseHelper.error(msg=msg, code=400)

    @staticmethod
    def unauthorized(msg: str = "未授权") -> dict:
        """401响应"""
        return ApiResponseHelper.error(msg=msg, code=401)

    @staticmethod
    def forbidden(msg: str = "禁止访问") -> dict:
        """403响应"""
        return ApiResponseHelper.error(msg=msg, code=403)

    @staticmethod
    def internal_error(msg: str = "系统内部错误") -> dict:
        """500响应"""
        return ApiResponseHelper.error(msg=msg, code=500)
